package com.huaxin.common.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.NumberPicker;

import com.huaxin.common.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


/**
 * @author songxf
 * @date 2018/6/11
 * @Desc 自定义简单日期选择
 * Copyright songxuefeng. All rights reserved.
 */
public class HxDateView extends LinearLayout implements NumberPicker.OnValueChangeListener {
    private NumberPicker yearPicker;
    private NumberPicker monthPicker;
    private SimpleDateFormat format       = new SimpleDateFormat("yyyy-MM");
    private Calendar         mCalendar    = Calendar.getInstance();
    private Calendar         tempCalendar = Calendar.getInstance();
    private Date             MIN_DATE     = new Date(0);
    private Date             MAX_DATE     = new Date();

    public void setOnSelectDateListener(onSelectDateListener onSelectDateListener) {
        this.mOnSelectDateListener = onSelectDateListener;
    }

    private onSelectDateListener mOnSelectDateListener;

    public interface onSelectDateListener {
        void setSelectDate(String data,boolean isSelect);
    }


    public HxDateView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_date, this, true);
        init();

    }

    private void init() {
        yearPicker = (NumberPicker) findViewById(R.id.year);
        monthPicker = (NumberPicker) findViewById(R.id.month);
        initDate();
    }

    private void initDate() {
        tempCalendar.setTime(MIN_DATE);
        yearPicker.setMinValue(tempCalendar.get(Calendar.YEAR));
        monthPicker.setMinValue(1);
        tempCalendar.setTime(MAX_DATE);
        yearPicker.setMaxValue(tempCalendar.get(Calendar.YEAR));
        monthPicker.setMaxValue(12);
        yearPicker.setWrapSelectorWheel(false);
        yearPicker.setOnValueChangedListener(this);
        monthPicker.setOnValueChangedListener(this);
        yearPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        monthPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        upDateView();
    }

    private void upDateView() {
        if (mCalendar.getTime().before(MIN_DATE)) {
            mCalendar.setTime(MIN_DATE);
        }
        if (mCalendar.getTime().after(MAX_DATE)) {
            mCalendar.setTime(MAX_DATE);
        }
        tempCalendar.setTime(MAX_DATE);
        if (mCalendar.get(Calendar.YEAR) == tempCalendar.get(Calendar.YEAR)) {
            monthPicker.setMaxValue(tempCalendar.get(Calendar.MONTH) + 1);
            monthPicker.setWrapSelectorWheel(false);
        } else {
            monthPicker.setMaxValue(12);
            monthPicker.setWrapSelectorWheel(true);
        }
        yearPicker.setValue(mCalendar.get(Calendar.YEAR));
        monthPicker.setValue(mCalendar.get(Calendar.MONTH) + 1);
        if (mOnSelectDateListener != null) {
            mOnSelectDateListener.setSelectDate(format.format(mCalendar.getTime()),true);
        }
    }

    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        if (picker.equals(monthPicker)) {
            if (oldVal == 1 && newVal == 12) {
                mCalendar.add(Calendar.MONTH, -1);
            } else if (oldVal == 12 && newVal == 1) {
                mCalendar.add(Calendar.MONTH, 1);
            } else {
                mCalendar.add(Calendar.MONTH, newVal - oldVal);
            }
            if (newVal == 1) {
                monthPicker.setWrapSelectorWheel(true);
            }
        } else if (picker.equals(yearPicker)) {
            mCalendar.set(Calendar.YEAR, newVal);
        }
        upDateView();
    }
}
